import { Component, OnInit, Input, EventEmitter, Output, Injector } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { Http } from '@angular/http';
import { TreeService } from 'app/public/serve/tree/tree.service';
import { Service } from 'app/server/server';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-base-tree',
  templateUrl: './base-tree.component.html',
  styleUrls: ['./base-tree.component.css']
})
export class BaseTreeComponent implements OnInit {
  protected treeSrv: TreeService;
  protected srv: Service;
  protected http: Http;
  protected route: ActivatedRoute
  protected router: Router
  constructor(
    injector: Injector
  ) {
    this.treeSrv = injector.get(TreeService);
    this.srv = injector.get(Service);
    this.http = injector.get(Http)
    this.route = injector.get(ActivatedRoute)
    this.router = injector.get(Router)
  }

  ngOnInit() {
    this.route
    this.router
  }
  valid: string = ''; // 如果不通过验证;
  data: TreeNode[];
  @Input() selectedFiles = [];
  @Output() selectedFilesChange: EventEmitter<any> = new EventEmitter();
  @Input() expressionError: boolean = false; // 是否提示用户输入
  nodeSelect($event) {
    this.selectedFilesChange.emit(this.selectedFiles)
    //console.log(this.data)
  }
  nodeUnselect($event) {
    this.selectedFilesChange.emit(this.selectedFiles)
  }
  /**
   * 
   */
  expandAll() {
    this.data.forEach(node => {
      this.expandRecursive(node, true);
    });
  }

  collapseAll() {
    this.data.forEach(node => {
      this.expandRecursive(node, false);
    });
  }
  public expandRecursive(node: TreeNode, isExpand: boolean) {
    node.expanded = isExpand;
    if (node.children) {
      node.children.forEach(childNode => {
        this.expandRecursive(childNode, isExpand);
        //console.log(childNode)
      });
    }
  }
  findInTree(trees: Array<any>, id: string, idName: string): any {
    // 不好但可行的做法
    // let result
    // for (let item of trees) {
    //   if (item[idName] == id) {
    //     result = item;
    //     break;
    //   } else if (item.children) {
    //     result = this.findInTree(item.children, id, idName)
    //     if (result) {
    //       break
    //     } else {
    //       continue
    //     }
    //   } else {
    //   }
    // }
    // return result
    //console.log(trees)
    let result;
    for (let tree of trees) {
      if (tree[idName] === id) {
        result = tree;
        break;
      }
    }
    if (result) return result;
    for (let tree of trees) {
      if (tree.children) {
        result = this.findInTree(tree.children, id, idName);
        if (result) break;
      }
    }
    return result;
  }

  expendToSelectedFiles() {

  }
  // 只展开子节点选中的父节点
  expandAllOnlyWithSelected(ids: Array<string>) {
    let expandAll = (data) => {
      if (data instanceof Array) {
        for (let item of data) {
          if (item.children) {
            item.children.forEach(_item => {
              if(ids.includes(_item.data)){
                item.expanded = true
              }
            })
            expandAll(item.children)
          }
        }
      }
    }
    expandAll(this.data)
    //console.log(this.data)
  }
}
